// miniprogram/pages/deviceControl/PM2.5/index.js

const { request } = require('../../API/request.js');



Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceInfo:{},
    deviceData:[],
    pm2_5:26,
    pm10:32,
    pm1_0:41
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    wx.setNavigationBarTitle({
      title: 'PM2.5监测',
    })
    // this.getServerData();
    this.getLastPageData();
    this.getDeviceAttribute();

  },

  //获取上一页的数据
  getLastPageData(){
    const that = this;
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('getDeviceInfo',(data)=>{
      that.setData({ deviceInfo:data })
    })
  },

  //获取设备属性数据
  async getDeviceAttribute(){
    const deviceInfo = this.data.deviceInfo;
    const res = await request(`system/device/${deviceInfo.deviceId}`,'get');
    console.log("设备详情",res);
    if (res.code === 200) {
      this.setData({
        deviceData:res.data
      })
    }
  },

  lookDetail(e){
    wx.navigateTo({
      url: '/pages/deviceDetail/index',
      success: (result) => {
        result.eventChannel.emit('getDeviceInfo',e.currentTarget.dataset.info)
      }
    })
  }
})